﻿'solo hacer editable la columna de los checks
'Buscas una ves, presionas el boton buscar o enter con el filtro vacio ya no busca nada
Imports System.Windows.Forms
Imports CapaNegocio
Public Class frmListaExamen
    Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
    Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
    Public Const SC_CLOSE = &HF060&
    Public Const MF_BYCOMMAND = &H0&
    Function RemoveXButton(ByVal iHWND As Integer) As Integer
        Dim iSysMenu As Integer
        iSysMenu = GetSystemMenu(iHWND, False)
        Return RemoveMenu(iSysMenu, SC_CLOSE, MF_BYCOMMAND)
    End Function

    'Variable para no perder el FOCUS

    Private Sub listartodos()
        DGVExamenes.DataSource = cE.mtdListarExamenesTodos
        DGVExamenes.AllowUserToAddRows = False
        DGVExamenes.AutoResizeColumns()
        DGVExamenes.AutoResizeRows()
        For i = 1 To 5
            DGVExamenes.Columns(i).ReadOnly = True
        Next
    End Sub

    Private Sub frmListaExamen_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        txtfiltro.Text = ""
        listartodos()
    End Sub

    Private Sub frmListaExamen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Deshabilitar boton [X]
        RemoveXButton(Me.Handle().ToInt32())
        'Para no perder el focus        
        txtfiltro.Text = ""
        listartodos()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarPaciente.Click

        Dim codigoboleta As Integer
        Dim flag As Boolean = False

        cB.fecreacion_Boleta = Date.Now.ToString("yyyy/MM/dd HH:mm:ss").ToString
        cB.estado_Boleta = 0
        cB.total_Boleta = 0.0
        cB.Paciente_codigo_paciente = cP.codigo_paciente
        'PEDIR A DIEGO QUE GUARDE EL CODIGO DEL USUARIO
        'ULTIMA MODIFICACION 29/01/2012        

        cB.Usuario_codigo_Usuario = cU.codLogueado
        cB.mtdCrearBoleta()
        cB.mtdBuscarUltimaBoleta()
        codigoboleta = cB.codigo_Boleta
        'MsgBox(codigoboleta)

        For Each Row As DataGridViewRow In DGVExamenes.Rows()
            'captura la segunda columna del la lista
            If (Row.Cells(0).Value = True) Then

                varCodEx = Row.Cells(1).Value
                'se envia las filas seleccionadas para crear una nueva boleta y 
                'comenzar con los examenes
                cB.cantidad_BoletaDetalle = 1
                cB.estado_BoletaDetalle = 0
                cB.fecestado_BoletaDetalle = Date.Now.ToString("yyyy/MM/dd HH:mm:ss").ToString
                cB.Boleta_codigo_Boleta = codigoboleta
                cB.Examen_codigo_Examen = varCodEx
                cB.mtdAñadirDetalleBoleta()
                flag = True
                'cB.mtdAñadirRespuestaDetalleBoleta()

            End If
        Next


        If flag = True Then
            DGVExamenes.DataSource = cE.mtdListarExamenesTodos
            examen.DGVPacienteExamen.DataSource = cB.ListarBoletas
            cP.mtdActualizarPacienteGestante()
            focusfrm = False
            Me.Hide()
        End If

       
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevoExamen.Click
        'CREANDO NUEVO EXAMEN
        '''''''''''''''''''''''''''''''
        Dim sis As New frmSistema
        'PROBANDO NUEVA INSTANCIA 
        Dim frmexamen As New frmAgregarEditarExamen 'nueva instancia del frmexamen, para crear nuevos examenes

        cE.nombre_Examen = ""
        cE.descripcion_Examen = ""
        cE.precio_Examen = 0.0
        'frmexamen.txtdescripcion.Text = ""
        'frmexamen.txtnombre.Text = ""
        'frmexamen.txtprecio.Text = "0.00"
        frmexamen.Show()
        frmexamen.btnEditarExamen.Enabled = False

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarExamen.Click
        If txtfiltro.Text = "" Or txtfiltro.Text = " " Then
            DGVExamenes.DataSource = cE.mtdListarExamenesTodos
            DGVExamenes.AllowUserToAddRows = False
            DGVExamenes.AutoResizeColumns()
            DGVExamenes.AutoResizeRows()
        Else
            cE.nombre_Examen = txtfiltro.Text
            DGVExamenes.DataSource = cE.BuscarExamenes
            DGVExamenes.AllowUserToAddRows = False
            DGVExamenes.AutoResizeColumns()
            DGVExamenes.AutoResizeRows()
        End If
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificarExamen.Click
        'EDITANDO EXAMEN
        '''''''''''''''''''''
        Dim mexamen As New frmExamen 'creamos nuevo formulario para modificar examenes seleccionados
        'Dim mexamen As New frmAgregarEditarExamen
        Dim aux As Integer = 0

        For Each Row As DataGridViewRow In DGVExamenes.Rows()
            'captura la segunda columna del la lista
            If (Row.Cells(0).Value = True) Then
                'If aux = 1 Then
                'End If
                varCodEx = Row.Cells(1).Value
                cE.codigo_Examen = varCodEx
                cE.mtdBuscarExamenCodigo()
                'aux = aux + 1
                mexamen.Show()
                mexamen.btnAgregarExamen.Enabled = False
                mexamen.cmbtipo.SelectedIndex = cE.ExamenTipo_codigo_ExamenTipo - 1 'para que aparesca el tipo ya seleccionado
                Exit For 'Sale del ForEACH
            End If
        Next
    End Sub

    'Private Sub FrmCerrando(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    '    'Me.Close()
    '    Me.Visible = False
    'End Sub


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        focusfrm = False
        Me.Visible = False
    End Sub


    Private Sub txtfiltro_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtfiltro.TextChanged

    End Sub
End Class